package com.learn.h;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDAF;
import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;
import org.apache.hadoop.io.IntWritable;

@Description(name="my_max",value="_FUNC_(Int) - Returns the maxinum value of Int number")
public class MaxIntUDAF extends UDAF{
	public static class MaxNumberIntUDAFEvaluator implements UDAFEvaluator {
		private IntWritable result;

		public void init() {
			result = null;
		}

		public boolean iterate(IntWritable value) {
			if (value ==null)
			{
				return false;
			}
			if (result==null)
			{
				result =new IntWritable(value.get());
			}
			else
			{
				 result.set(Math.max(result.get(), value.get()));
			}
			return true;
			
		}

		public IntWritable terminatePartial() {
			return result;
		}

		public boolean merge(IntWritable other) {
			return iterate(other);
		}

		public IntWritable terminate() {
			return result;
		}
	}

}
